package com.qiyuan.boot.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiyuan.boot.utils.BusinessException;
import com.qiyuan.boot.utils.StringUtils;
import com.qiyuan.boot.utils.entity.UploadResult;
import com.qiyuan.boot.utils.oss.QcloudOssUtil;
import com.qiyuan.boot.web.bean.SysOss;
import com.qiyuan.boot.web.bean.SysOssConfig;
import com.qiyuan.boot.web.mapper.SysOssConfigMapper;
import com.qiyuan.boot.web.mapper.SysOssMapper;
import com.qiyuan.boot.web.service.SysOssService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiyuan
 * @time 2022-03-28
 */
@Service
public class SysOssServiceImpl extends ServiceImpl<SysOssMapper, SysOss> implements SysOssService {

    @Resource
    private SysOssMapper sysOssMapper;
    @Resource
    private SysOssConfigMapper configMapper;

    @Override
    public SysOss upload(MultipartFile file) {
        SysOssConfig sysOssConfig = sysOssConfigInfo();
        String originalfileName = file.getOriginalFilename();
        String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());

        QcloudOssUtil ossUtil = new QcloudOssUtil(
                sysOssConfig.getEndpoint(),
                sysOssConfig.getPrefix(),
                sysOssConfig.getAccessKey(),
                sysOssConfig.getSecretKey(),
                sysOssConfig.getBucketName(),
                sysOssConfig.getRegion(),
                sysOssConfig.getIsHttps(),
                sysOssConfig.getIntranet(),
                sysOssConfig.getIntranetStatus()
        );
        ossUtil.init();
        UploadResult uploadResult = null;
        try {
            uploadResult = ossUtil.uploadSuffix(file.getBytes(),suffix,file.getContentType());
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 保存文件信息
        SysOss oss = new SysOss()
                .setUrl(uploadResult.getUrl())
                .setIntranetUrl(uploadResult.getIntranetUrl())
                .setIntranetStatus(uploadResult.getIntranetStatus())
                .setFileSuffix(suffix)
                .setFileName(uploadResult.getFilename())
                .setOriginalName(originalfileName)
                .setCreate_time(new Date())
                .setUpdateTime(new Date())
                .setCreateBy("前端")
                .setService("qcloud");
        save(oss);
        return oss;
    }


    public SysOssConfig sysOssConfigInfo(){
        QueryWrapper<SysOssConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysOssConfig::getStatus,"0");
        SysOssConfig sysOssConfig = configMapper.selectOne(queryWrapper);
        if (null == sysOssConfig){
            throw new BusinessException("上传文件异常");
        }
        return sysOssConfig;
    }

}
